package nil.ed.simulator.request.biz.qq.listener;

import com.google.inject.Inject;
import lombok.Setter;
import lombok.SneakyThrows;
import nil.ed.simulator.request.common.QQItem;
import nil.ed.simulator.request.enums.events.EventMode;
import nil.ed.simulator.request.enums.events.EventType;
import nil.ed.simulator.request.event.Event;
import nil.ed.simulator.request.event.EventBusAware;
import nil.ed.simulator.request.event.bus.EventBus;
import nil.ed.simulator.request.event.bus.EventListener;
import nil.ed.simulator.request.parser.IParser;
import nil.ed.simulator.request.repo.RuntimeRepo;

import java.io.File;
import java.util.List;

/**
 * @author lidelin.
 */
public class ImportQqFileLister implements EventListener, EventBusAware {

    private final IParser parser;
    @Setter
    private EventBus eventBus;

    @Inject
    public ImportQqFileLister(IParser parser) {
        this.parser = parser;
    }

    @Override
    @SuppressWarnings("unchecked")
    @SneakyThrows
    public void onEvent(Event event) {
        List<File> fs = (List<File>) event.getData();
        File f = fs.get(0);
        RuntimeRepo.QQ_REPO.load(f.getAbsolutePath(), parser);
        List<QQItem> qqItemList = RuntimeRepo.QQ_REPO.getQqList();
        eventBus.publish(Event.builder()
                .source(null)
                .eventMode(EventMode.ASYNC)
                .data(qqItemList)
                .eventType(EventType.PARSE_SUCCESS)
                .build());
    }

    @Override
    public boolean isSupport(Event event) {
        return event.getEventType() == EventType.FILE_IMPORT;
    }
}
